package vn.com.bookstore.activity;

import java.util.ArrayList;

import vn.com.bookstore.Config;
import vn.com.bookstore.ConnectActivity;
import vn.com.bookstore.R;
import vn.com.bookstore.adapter.ChapterAdapter;
import vn.com.bookstore.database.smng.BookMarkMng;
import vn.com.bookstore.database.sobject.BookMark;
import vn.com.bookstore.model.Book;
import vn.com.bookstore.model.ListPage;
import vn.com.bookstore.model.Page;
import vn.com.bookstore.network.NetworkAdapter;
import vn.com.bookstore.network.NetworkAdapter.OnNetworkListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

public class ChapterActivity extends ConnectActivity implements OnItemClickListener, OnNetworkListener {
	ListView lvChapter;
	protected LinearLayout mLayoutLoading;
	protected View mNoData;
	ChapterAdapter chapterAdapter;

	Book book;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_chapter);

		Bundle bundle = this.getIntent().getExtras();
		if (null != bundle) {
			if (bundle.containsKey(Book.TAG))
				book = (Book) bundle.getSerializable(Book.TAG);
		}

		if (book == null) {
			finish();
			return;
		}

		setTitle(book.title, book.author);

		findViewById();

		chapterAdapter = new ChapterAdapter(this, book);
		lvChapter.setAdapter(chapterAdapter);

		lvChapter.setOnItemClickListener(this);

		loadData(false);
		// book = Book.createDataSample(2);
		chapterAdapter.setData(book);
	}

	public void loadData(final boolean forceLoad) {
		// get list of Chapter
		new Thread() {
			public void run() {
				setLoading(true);
				NetworkAdapter net = new NetworkAdapter(ChapterActivity.this, mHandler, ChapterActivity.this);
				Config.NetObject obj = Config.BookStore.getUrlListChap(book.id, 1, Config.LIMIT);
				net.sendHttpGet(ConnectActivity.METHOD_GET_PAGE, obj.url, obj.param, forceLoad);
			};
		}.start();
	}

	@Override
	protected void onResume() {
		super.onResume();
		
	}

	@Override
	protected void findViewById() {
		super.findViewById();
		lvChapter = (ListView) findViewById(R.id.list_chapter);
		mLayoutLoading = (LinearLayout) findViewById(R.id.layout_loading);
		mNoData = findViewById(R.id.tv_nodata);
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View itemView, int position, long itemId) {
		Page currentPage = book.pages.get(position);
		startViewBook(currentPage, book, position);
	}

	public void startViewBook(Page currentPage, Book book, int position) {
		Intent i = new Intent(this, BookViewActivity.class);
		i.putExtra(Book.TAG, book);
		i.putExtra("position", position);
		i.putExtra(Page.TAG, currentPage);
		startActivity(i);
	}

	@Override
	public void onResponse(int idMethod, boolean isSuccess, String json) {
		setLoading(false);
		if (isSuccess) {
			switch (idMethod) {
			case METHOD_GET_PAGE:
				ArrayList<Page> page = new ArrayList<Page>(ListPage.parseListGson(json));
				book.pages = new ArrayList<Page>(page);
				chapterAdapter.setData(book);
				try {
					if (book.pages.size() == 1) {
						startViewBook(book.pages.get(0), book, 0);
//						finish();
					}
					else{
						BookMarkMng mng =new BookMarkMng(this);
						ArrayList<BookMark> lstBookMark = mng.select(BookMarkMng.bookId, book.id+"");
						if(lstBookMark==null || lstBookMark.isEmpty()){
							
						}else{
								for(Page item: book.pages){
									for(BookMark itemBM: lstBookMark){
										if(itemBM.pageId.equals(item.id+"")){
											startViewBook(item, book, Integer.valueOf(itemBM.position));
											finish();
											break;
										}
									}
								}
						}
					}
				} catch (Exception e) {
				}
				
				break;
			case METHOD_GET_IMAGE:
				Log.e("", "download success");
				break;
			default:
				break;
			}
		}
		refreshUI();
	}

	protected void refreshUI() {
		if (chapterAdapter.getCount() == 0) {
			lvChapter.setVisibility(View.GONE);
		} else {
			lvChapter.setVisibility(View.VISIBLE);
		}
	}

	public void setLoading(final boolean isLoading) {
		mLayoutLoading.post(new Runnable() {

			@Override
			public void run() {

				mLayoutLoading.setVisibility(View.GONE);
				if (isLoading) {
					mLayoutLoading.setVisibility(View.VISIBLE);
					mNoData.setVisibility(View.GONE);
				} else {
					refreshUI();
				}
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_home, menu);
		return true;
	}

	public void mnClicked(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.help:
			startActivity(new Intent(this, HelpActivity.class));
			break;
		case R.id.refresh:
			loadData(true);
			break;
		default:
			break;
		}

	}
}
